<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
<span id='global-property-'>/**
</span> * Cube PHP Framework
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the &quot;License&quot;); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * @author Dillen / Steffen
 */

namespace Library;
 
<span id='Library-Application'>/**
</span> * The application
 * 
 * @class Library.Application
 */
class Application
{
<span id='Library-Application-property-config'>	/**
</span>	 * Config instance
	 * 
	 * @property {Library.Application.Config} config
	 */
	public $config;
	
<span id='Library-Application-property-_bootstrap'>	/**
</span>	 * Holds the Bootstrap instance
	 * 
	 * @property {Library.Mvc.Bootstrap} _bootstrap
	 * @private
	 */
	private $_bootstrap;
	
<span id='Library-Application-property-_autoloader'>	/**
</span>	 * Autoloader instance
	 * 
	 * @property {Library.Autoloader} _autoloader
	 * @private
	 */
	private $_autoloader;
	
<span id='Library-Application-static-property-'>	/**
</span>	 * Application singleton instance
	 * 
	 * @property {Library.Application}
	 * @var Application
	 * @private
	 * @static
	 */
	private static $_instance;
	
<span id='Library-Application-static-method-getInstance'>	/**
</span>	 * Get the current application instance
	 * 
	 * @method getInstance
	 * @static
	 */
	public static function getInstance()
	{
		if (!self::$_instance instanceof Application)
		{
			throw new \Exception('Application instance not found, current type is ' . gettype(self::$_instance) . '.');
		}
		
		return self::$_instance;
	}
	
<span id='Library-Application-method-__construct'>	/**
</span>	 * Constructor
	 *
	 * Create a new application
	 * 
	 * @method __construct
	 * @param {String} $module Module name
	 * @param {String} [$path=null] Module path
	 */
	public function __construct($module, $path = null)
	{
		self::$_instance = $this;
		
		if ($path === null)
		{
			$path = 'modules/' . $module;
		}
		
		$this-&gt;_setGlobalSettings();
		
		if (!file_exists($path . '/config/settings.ini'))
		{
			throw new \Exception('Settings file ' . $path . '/config/settings.ini does not exist.');
		}
		
		$ini = new \Library\Config\Ini($path . '/config/settings.ini');
		
		$this-&gt;config = new ArrayObject($ini-&gt;getData());
		
		$this-&gt;config['application']['module'] = $module;
		$this-&gt;config['application']['path'] = rtrim($path, '/') . '/';
	}

<span id='Library-Application-method-_setGlobalSettings'>	/**
</span>	 * Sets up the autoloader and other neccesary global settings
	 * 
	 * @method _setGlobalSettings
	 * @private
	 */
	private function _setGlobalSettings()
	{
		error_reporting(E_ALL);
		ini_set('display_errors', '1');
		
		//Register autoloader and error handlers
		$this-&gt;_autoloader = new Autoloader();
		
		spl_autoload_register(array($this-&gt;_autoloader, 'autoload'));
		set_error_handler(function($errno, $errstr, $errfile, $errline ) {
		    throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
		});
	}
	
<span id='Library-Application-method-run'>	/**
</span>	 * Run application
	 * 
	 * @method run
	 */
	public function run()
	{
		//Run output handler
		ob_start();
		
		//Register application namespaces
		$this-&gt;_autoloader-&gt;registerNamespace($this-&gt;config['application']['module'] . '\\Controllers', $this-&gt;config['application']['path']. '/controllers/');
		$this-&gt;_autoloader-&gt;registerNamespace($this-&gt;config['application']['module'] . '\\Events', $this-&gt;config['application']['path']. '/events/');
		$this-&gt;_autoloader-&gt;registerNamespace($this-&gt;config['application']['module'] . '\\Models', $this-&gt;config['application']['path']. '/models/');
		
		if (file_exists($this-&gt;config['application']['path'] . '/Bootstrap.php'))
		{
			include( $this-&gt;config['application']['path'] . '/Bootstrap.php' );
			
			$class = '\\' . $this-&gt;config['application']['module'] . '\\Bootstrap';
		}
		else 
		{
			$class = '\\Library\\Mvc\\Bootstrap';
		}
		
		$this-&gt;_bootstrap = new $class($this);
	}
}</pre>
</body>
</html>
